//
//  BmobManager.swift
//  BarCode
//
//  Created by xxcao on 2017/4/1.
//  Copyright © 2017年 MobileArtisan. All rights reserved.
//

import UIKit
import Foundation

enum NetworkResultStatus : String{
    case NRSSuccess = "NetworkResultStatusSuccess"
    case NRSFailure = "NetworkResultStatusFailure"
    case NRSOffline = "NetworkResultStatusOffline"
    
}
class BmobManager: NSObject {
    //模拟注册
    class func mockRegister () {
        
        var user = BmobUser()
        user.username = "13400074008"
        user.password = "111111"
        user.setObject("王力汉", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        user = BmobUser()
        user.username = "18662724884"
        user.password = "111111"
        user.setObject("曹兴星", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18094221200"
        user.password = "111111"
        user.setObject("邱梦芸", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18395201500"
        user.password = "111111"
        user.setObject("李子博", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13027778807"
        user.password = "111111"
        user.setObject("陈杰", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18652979542"
        user.password = "111111"
        user.setObject("赵瑞", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18516735115"
        user.password = "111111"
        user.setObject("陆意全", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        
        user = BmobUser()
        user.username = "13338675893"
        user.password = "111111"
        user.setObject("刘敏", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        
        user = BmobUser()
        user.username = "18952028959"
        user.password = "111111"
        user.setObject("彭鹏", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "15380964428"
        user.password = "111111"
        user.setObject("魏鑫", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "15195955838"
        user.password = "111111"
        user.setObject("杨坚", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13675112958"
        user.password = "111111"
        user.setObject("董宇鹏", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "15651968717"
        user.password = "111111"
        user.setObject("卞克", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "15895910907"
        user.password = "111111"
        user.setObject("朱丽楠", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13915995280"
        user.password = "111111"
        user.setObject("刘敏", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13852297081"
        user.password = "111111"
        user.setObject("梁伟", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13818057849"
        user.password = "111111"
        user.setObject("严必庆", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18512503787"
        user.password = "111111"
        user.setObject("刘鹏", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18951819782"
        user.password = "111111"
        user.setObject("董杰", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18021417499"
        user.password = "111111"
        user.setObject("唐小绪", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18061467741"
        user.password = "111111"
        user.setObject("林琳", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "15150596942"
        user.password = "111111"
        user.setObject("叶潇", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "15738776538"
        user.password = "111111"
        user.setObject("汪坤呈", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18810905361"
        user.password = "111111"
        user.setObject("刘佳佳", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "17600123655"
        user.password = "111111"
        user.setObject("柴俊", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "15951816842"
        user.password = "111111"
        user.setObject("叶果", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13675150740"
        user.password = "111111"
        user.setObject("俞弦", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18651631820"
        user.password = "111111"
        user.setObject("焦兰兰", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13810248633"
        user.password = "111111"
        user.setObject("严辉", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        
        user = BmobUser()
        user.username = "15861097927"
        user.password = "111111"
        user.setObject("齐云猛", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13813827385"
        user.password = "111111"
        user.setObject("戈程林", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18801126073"
        user.password = "111111"
        user.setObject("高弯弯", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13951612245"
        user.password = "111111"
        user.setObject("马志刚", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18019937002"
        user.password = "111111"
        user.setObject("韩帅帅", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13520246856"
        user.password = "111111"
        user.setObject("李广", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13817101266"
        user.password = "111111"
        user.setObject("贾俊超", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13813065238"
        user.password = "111111"
        user.setObject("张春平", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18512553258"
        user.password = "111111"
        user.setObject("徐华", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "18611244930"
        user.password = "111111"
        user.setObject("张兴华", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "15910881533"
        user.password = "111111"
        user.setObject("崔涛涛", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
        
        
        user = BmobUser()
        user.username = "13262965657"
        user.password = "111111"
        user.setObject("阚强", forKey: "full_name")
        user.signUpInBackground { (isSuccessful, error) in
            if(isSuccessful) {
                print("Sign up successfully")
            } else {
                print(error as Any)
            }
        }
    }
    
    //登录接口
    class func login(userName:String, psdword:String, result:@escaping (_ res:Any?, _ status:NetworkResultStatus)->(Void)) {
        BmobUser.loginInbackground(withAccount: userName, andPassword: psdword) { (user, error) in
            if(user != nil) {
                print(user!)
                let dic = ["username":user?.username,
                           "objectId":user?.objectId,
                           "full_name":user?.object(forKey: "full_name"),
                           "role":user?.object(forKey: "role"),
                           "staff_code":user?.object(forKey: "staff_code"),
                           "email":user?.object(forKey: "email")]
                result(dic, .NRSSuccess)
            }
            else {
                print(error!)
                result(error?.localizedDescription, .NRSFailure)
            }
        }
    }
    
    //登出接口
    class func logout(result:(_ res:Any?, _ status:NetworkResultStatus)->(Void)) {
        BmobUser.logout()
    }
    
    //查询是否存在记录
    class func isExistRecord(table:String, key:String, value:String, result:@escaping (_ res:Any?, _ status:NetworkResultStatus)->(Void)){
        let bquery = BmobQuery(className: table)
        bquery?.whereKey(key, equalTo: value)
        bquery?.whereKey("counts", greaterThan: NSNumber(value: 0))
        bquery?.findObjectsInBackground({ (results, error) in
            if error != nil {
                result(error?.localizedDescription, .NRSFailure)
            } else {
                result(results, .NRSSuccess);
            }
        })
    }
    
    //绑定接口
    class func binding(userName:String, fullName:String, department:String, code:String, goodsName:String, result:@escaping (_ res:Any?, _ status:NetworkResultStatus)->(Void)) {
        let user2codeObj = BmobObject(className: "user2code")
        user2codeObj?.setObject(userName, forKey: "user_id")
        user2codeObj?.setObject(fullName, forKey: "full_name")
        user2codeObj?.setObject(department, forKey: "department")
        user2codeObj?.setObject(code, forKey: "code")
        user2codeObj?.setObject(goodsName, forKey: "goods_name")
        user2codeObj?.saveInBackground(resultBlock: { (isSuccessful, error) in
            if(error == nil) {
                if (isSuccessful) {
                    result("绑定成功", .NRSSuccess)
                } else {
                    result("绑定失败", .NRSFailure)
                }
            } else {
                result(error?.localizedDescription, .NRSFailure)
            }
        })
    }
    
    //更新库存
    class func updateStock(goods_code: String, result:@escaping (_ res:Any?, _ status:NetworkResultStatus)->(Void)) {
        let bquery = BmobQuery(className: "goods");
        bquery?.whereKey("goods_code", equalTo: goods_code)
        bquery?.findObjectsInBackground({ (results, error) in
            if (error == nil){
                if (results != nil && (results?.count)! > 0){
                    let object = results?.first as AnyObject
                    let counts = (object.object(forKey: "counts") as! NSNumber).intValue
                    if (counts >= 1) {
                        object.setObject(NSNumber(value: counts - 1), forKey: "counts")
                        object.updateInBackground()
                        result("更新库存成功", .NRSSuccess)
                    } else {
                        result("库存不足，无法更新", .NRSFailure)
                    }
                } else {
                    result("没有此商品", .NRSFailure)
                }
            }
            else {
                result(error?.localizedDescription, .NRSFailure)
            }
        })
    }
    
    //修改密码
    class func modifyPsdword(oldPsdword:String, newPsdword:String, result:@escaping (_ res:Any?, _ status:NetworkResultStatus)->(Void)) {
        let user = BmobUser.current()
        user?.updateCurrentUserPassword(withOldPassword: oldPsdword, newPassword: newPsdword, block: { (isSuccessful, error) in
            if isSuccessful {
                result("修改成功，请重新登录", .NRSSuccess)
//                BmobUser.loginWithUsername(inBackground: user?.username, password: newPsdword, block: { (user1, err) in
//                    if err != nil {
//                        print("\(String(describing: user1))")
//                        
//                    }else{
//                        print("login error \(String(describing: err))")
//                    }
//                })
            } else {
                result(error?.localizedDescription, .NRSFailure)
            }
        })
    }
    
    //获取人员接口
    class func getUsers(result: @escaping (_ res:Any?, _ status:NetworkResultStatus)->(Void)) {
        
        let bquery = BmobQuery(className: "person")
        bquery?.limit = 1000
        bquery?.findObjectsInBackground({ (results, error) in
            if(error == nil) {
                print(results as Any)
                userList.removeAll(keepingCapacity: true)
                for result in results! {
                    let tmpUser = User()
                    let dic = ["username":(result as AnyObject).object(forKey: "username"),
                               "objectId":(result as AnyObject).objectId,
                               "full_name":(result as AnyObject).object(forKey: "full_name"),
                               "role":(result as AnyObject).object(forKey: "role"),
                               "staff_code":(result as AnyObject).object(forKey: "staff_code")]
                    tmpUser.transeferDic2User(dic: dic as Any as! Dictionary<String, Any>)
                    userList.append(tmpUser)
                }
                result(userList, .NRSSuccess)
            } else {
                result(error?.localizedDescription, .NRSFailure)
            }
        })
    }
        
    //版本检测
    class func checkAppVersion(result:@escaping (_ res:Any?, _ status:NetworkResultStatus)->(Void)) {
        let query:BmobQuery = BmobQuery(className: TABLE_TT_GlobalSetting)
        query.findObjectsInBackground { (array, error) in
            if (error != nil) {
                result(error?.localizedDescription, .NRSFailure)
            } else {
                if (array?.count != 0) == true {
                    let objData = array?[0] as! BmobObject
                    let serverVers = objData.object(forKey: "cur_version") as! String
                    result(serverVers, .NRSSuccess)
                } else {
                    result("获取版本号失败", .NRSFailure)
                }
            }
        }
    }
}
